package com.mall.controller;

import com.mall.entity.ShoppingCart;
import com.mall.service.ShoppingCartService;
import com.mall.vo.ResultVO;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/shopcat")
public class ShopcatController {
    //@GetMapping("/list")
    public ResultVO listCarts(){
        System.out.println("------购物车列表接口----------");
        return ResultVO.success("获取信息成功");
    }

    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     * @param cart 购物车对象
     * @param token
     * @return
     */
    @PostMapping("/add")
    public ResultVO addShoppingCart(@RequestBody ShoppingCart cart, @RequestHeader("token")String token){
        ResultVO resultVO=shoppingCartService.addShoppingCart(cart);
        return resultVO;
    }

    /**
     *
     * @param userId 用户id
     * @param token
     * @return
     */
    @GetMapping("/list")
    public ResultVO shoppingcartList(Integer userId,@RequestHeader("token") String token){
            return shoppingCartService.listShoppingCartsByUserId(userId);
    }

    /**
     * 更新购物车商品数量
     * @param cartId 购物车id
     * @param cartNum 要更改成的商品数量
     * @return
     */
    @PutMapping("/update/{cartid}/{cartnum}")
    public ResultVO updateNum(@PathVariable("cartid") Integer cartId,
                              @PathVariable("cartnum") Integer cartNum,
                            @RequestHeader("token") String token){
       return shoppingCartService.updateCartNumByCartId(cartId, cartNum);
    }

    @DeleteMapping("/delete/{cartid}")
    public ResultVO deleteCartRecordBycartId(@PathVariable("cartid") Integer cartId,@RequestHeader("token") String token){
        return shoppingCartService.deleteCartRecordByCartId(cartId);
    }

    /**
     * 订单结算获取用户购物车列表信息
     * @param cids
     * @param token
     * @return
     */
    @GetMapping("/listbycids")
    public ResultVO listShoppingCartsByCids(Integer[] cids,@RequestHeader("token") String token){
        return shoppingCartService.listShoppingCartsByCids(cids);
    }
}
